import { AxiosResponse } from "axios";
import { net } from "@/request";
import { AxiosRequestHeaders } from "axios";
import {ApiResult} from "@/request/types/base";

/**
 * 请求头的扩展
 */
interface RequestHeaders extends AxiosRequestHeaders {
    //有该请求头本次请求不会携带token
    isNotNeedToken?: boolean,
    //有该请求头本次请求回返回原始data且不会判断code
    needOriginData?: boolean
}

export function Post<T>(url: string, data?: any, headers?: RequestHeaders): Promise<ApiResult<T>> {
    return new Promise((resolve, reject) => {
        net.post<null, AxiosResponse<T, null>, null>(url, data, { headers }).then(res => {
            // @ts-ignore
            resolve(res)
        }).catch(err => {
            reject(err);
        })
    })
}

export function Delete<T>(url: string, data?: any, headers?: RequestHeaders): Promise<ApiResult<T>> {
    return new Promise((resolve, reject) => {
        net.delete<null, AxiosResponse<T, null>, null>(url, { headers, data }).then(res => {
            // @ts-ignore
            resolve(res)
        }).catch(err => {
            reject(err);
        })
    })
}

export function Put<T>(url: string, data?: any, headers?: RequestHeaders): Promise<ApiResult<T>> {
    return new Promise((resolve, reject) => {
        net.put<null, AxiosResponse<T, null>, null>(url, data, { headers }).then(res => {
            // @ts-ignore
            resolve(res)
        }).catch(err => {
            reject(err);
        })
    })
}

export function Get<T>(url: string, params?: any, headers?: RequestHeaders): Promise<ApiResult<T>>{
    return new Promise((resolve, reject) => {
        net.get<null, AxiosResponse<T, null>, null>(url, {
            params,
            headers
        })
            .then(res => {
                // @ts-ignore
                resolve(res)
            })
            .catch(err => {
                reject(err)
            })
    })
}
